package com.ruoyi.web.controller.storage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.storage.bicycle.domain.Bicycle;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.storage.tickets.service.ITicketsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产工单Controller
 * 
 * @author zzx
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/storage/tickets")
public class TicketsController extends BaseController
{
    @Autowired
    private ITicketsService ticketsService;

    /**
     * 查询生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tickets tickets)
    {
        startPage();
        List<Tickets> list = ticketsService.selectTicketsList(tickets);
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:export')")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tickets tickets)
    {
        List<Tickets> list = ticketsService.selectTicketsList(tickets);
        ExcelUtil<Tickets> util = new ExcelUtil<Tickets>(Tickets.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:query')")
    @GetMapping(value = "/{ticketsId}")
    public AjaxResult getInfo(@PathVariable("ticketsId") String ticketsId)
    {
        return success(ticketsService.selectTicketsByTicketsId(ticketsId));
    }

    /**
     * 新增生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:add')")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tickets tickets)
    {
        return toAjax(ticketsService.insertTickets(tickets));
    }

    /**
     * 修改生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tickets tickets)
    {
        return toAjax(ticketsService.updateTickets(tickets));
    }

    /**
     * 删除生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:remove')")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ticketsIds}")
    public AjaxResult remove(@PathVariable String[] ticketsIds)
    {
        return ticketsService.deleteTicketsByTicketsIds(ticketsIds);
    }
    /**
     * 审核生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{ticketsId}")
    public AjaxResult updateAudit(@PathVariable("ticketsId") String ticketsId)
    {
        return ticketsService.updateAudit(ticketsId);
    }
    /**
     * 结案/反结案生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping("/close/{ticketsId}")
    public AjaxResult updateClose(@PathVariable("ticketsId") String ticketsId)
    {
        return ticketsService.updateClose(ticketsId);
    }
    /**
     * 获取未打印车辆的数量
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:query')")
    @GetMapping("/unprintnumber/{ticketsId}")
    public AjaxResult getUnPrintNumber(@PathVariable("ticketsId") String ticketsId)
    {
        return ticketsService.selectUnPrintNumberByTicketsId(ticketsId);
    }
    /**
     * 生成车辆ID
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:add')")
    @Log(title = "车辆ID", businessType = BusinessType.INSERT)
    @PostMapping("/bicycle")
    public AjaxResult addBicycle(@RequestBody Map<String,Object> map)
    {

        return ticketsService.addBicycleByticketsId((String) map.get("ticketsId"),(Integer) map.get("printnumber"));

    }

    /**
     * 获取已打印车辆ID
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:query')")
    @GetMapping("/bicycle/{ticketsId}")
    public AjaxResult getBicycleList(@PathVariable("ticketsId") String ticketsId)
    {
        List<Bicycle> list =  ticketsService.selectBicycleByTicketsId(ticketsId);
        return AjaxResult.success(list);
    }
    /**
     * 修改生产工单
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping("/productLine")
    public AjaxResult editproductLine(@RequestBody Map<String,Object> map)
    {
        return ticketsService.updateTicketsByproductLine((ArrayList<String>) map.get("ticketsIds"),(String) map.get("productLine"));
    }
    /**
     * 补打车辆ID
     */
    @PreAuthorize("@ss.hasPermi('storage:tickets:add')")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping("/Clearing")
    public AjaxResult addBicycleOne(@RequestBody Map<String,Object> map)
    {
        return toAjax(ticketsService.insertBicycle((String) map.get("ticketsId"),(String)map.get("bicycleId")));
    }

}
